'use strict';
app.factory('UserService', function(HttpUtilService,$q,APPCONSTANT) {
        /***
         * 查询个人信息
         * @param token
         * @param userId
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function getUserInfo (token,userId){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("User/Self",{access_token:token,user_id:userId},token)
                .then(function (response) {
                    //todo:这里还要验证一下数据格式，文档写的不全
                    if(response.code == "000000"){
                        deferred.resolve(response.data)
                    } else {
                        console.log("getUserInfo:"+ response)
                    }
                },function (error) {
                    console.log("getUserInfo error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 注册用户
         * @param name
         * @param password
         * @param code
         * @param type
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function registerUser(name,password,code,type){
            var deferred = $q.defer();
            HttpUtilService.httpPostNoToken("User/Resister",{name:name,password:password,code:code,type:type})
                .then(function (response) {
                    //todo:这里还要验证一下数据格式，文档写的不全
                    if(response.code == "000000"){
                        deferred.resolve(response.data)
                    } else {
                        console.log("registerUser code:"+ response.code)
                    }
                },function (error) {
                    console.log("registerUser error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 账号安全 修改账号安全信息包括登录密码、绑定的邮箱、手机、以及支付密码
         * @param userId
         * @param password
         * @param token
         * @param type 1、登录密码修改;2、邮箱修改;3、电话修改;4、支付密码修改
         * @param message 新的字段信息，指要修改的信息
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function modifyUser(userId,password,token,type,message){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("Token/Request",{user_id:userId,password:password,type:type,message:message},token)
                .then(function (response) {
                    //todo:这里还要验证一下数据格式，文档写的不全
                    if(response.code == "000000"){
                        deferred.resolve(response.data)
                    } else {
                        console.log("modifyUser:"+ response)
                    }
                },function (error) {
                    console.log("modifyUser error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 密码找回
         * @param name
         * @param password
         * @param code
         * @param type
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function findPasssword(name,password,code,type){
            var deferred = $q.defer();
            HttpUtilService.httpPostNoToken("Version/Get",{name:name,password:password,code:code,type:type})
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.data)
                    } else {
                        console.log("findPasssword code:"+ response.code)
                    }
                },function (error) {
                    console.log("findPasssword error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 记录历史足迹
         * @param userId
         * @param goodId
         * @param catId
         * @param token
         */
        function marksUser(userId,goodId,catId,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("User/Marks",{user_id:userId,goods_id:goodId,cat_id:catId},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.data)
                    } else {
                        console.log("marksUser code:"+ response.code)
                    }
                },function (error) {
                    console.log("marksUser error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 用户实名制
         * @param userId
         * @param name
         * @param identityCard
         * @param token
         * response 是否成功（1：成功；0：未成功；-1：异常或不存在该请求）
         */
        function realInfoUser(userId,name,identityCard,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("User/RealInfo",{user_id:userId,name:name,idenity_card:identityCard},token)
                .then(function (response) {
                   deferred.resolve(response.data)
                },function (error) {
                    console.log("realInfoUser error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 上传用户头像
         * @param userId
         * @param photo_stream
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function uploadHeadImg(userId,photo_stream,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("User/Upload?user_id="+userId,{user_id:userId,photo_stream:photo_stream},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.head_ico)
                    } else {
                        console.log("uploadHeadImg code:"+ response.code)
                    }
                },function (error) {
                    console.log("uploadHeadImg error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 获取用户所有收货地址
         * @param userId
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function getAddressInfo(userId,token){
            var deferred = $q.defer();
            HttpUtilService.httpGetHasToken("Address/Query",{user_id:userId},token)
                .then(function (response) {
                deferred.resolve(response.data)
                },function (error) {
                    console.log("getAddressInfo error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 删除收货地址
         * @param address_id
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function delAddress(address_id,token){
            var deferred = $q.defer();
            HttpUtilService.httpGetHasToken("Address/Del",{address_id:address_id},token)
                .then(function (response) {
                deferred.resolve(response.data)
                },function (error) {
                    console.log("delAddress error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 添加地址
         * @param user_id
         * @param name 收货人
         * @param province int 省    请注意这几个参数是int类型
         * @param city int 市
         * @param area int 区
         * @param address_detail 地址详情
         * @param zip_code int 邮编
         * @param is_default 是否默认地址 bool
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function addAddress(user_id,name,province,city,area,address_detail,zip_code,is_default,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("Address/Del",{user_id:user_id,name:name,province:province,city:city,area:area,address_detail:address_detail,zip_code:zip_code,is_default:is_default},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.address_id)
                    } else {
                        console.log("addAddress code:"+ response.code)
                    }
                },function (error) {
                    console.log("addAddress error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 修改地址   参数同上
         * @param address_id
         * @param name
         * @param province
         * @param city
         * @param area
         * @param address_detail
         * @param zip_code
         * @param is_default
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function resetAddress(address_id,name,province,city,area,address_detail,zip_code,is_default,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("Address/Del",{address_id:address_id,name:name,province:province,city:city,area:area,address_detail:address_detail,zip_code:zip_code,is_default:is_default},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.response)
                    } else {
                        console.log("resetAddress code:"+ response.code)
                    }
                },function (error) {
                    console.log("resetAddress error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 获取用户订单数据
         * @param user_id
         * @param status int 0:待付款；1:已发货；2:待收货；3:待评论；4:退款/售后
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         * 返回值里有一个goods_array是一个jsonstrong 商品和货品名称 name和规格 value 串 json数据格式({"name":"法国进口红酒 塞莱斯城堡干红葡萄酒 整箱装 750ml*6瓶","goodsno":"2207667","value":"颜色:红"})
         */
        function getOrderInfo(user_id,status,token){
            var deferred = $q.defer();
            HttpUtilService.httpGetHasToken("Order/Query",{user_id:user_id,status:status},token)
                .then(function (response) {
                deferred.resolve(response.data)
                },function (error) {
                    console.log("delAddress error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 确定订单
         * @param userId 会员编号
         * @param seller_id 商家ID int
         * @param goods_id 商品ID int
         * @param goods_array 商品和货品名称 name和规格 value 串 json数据格式({"name":"法国进口红酒 塞莱斯城堡干红葡萄酒 整箱装 750ml*6瓶","goodsno":"2207667","value":"颜色:红"})
         * @param goods_nums 商品数量 int
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         * {
         * order_id int 订单ID
         * real_price float 实付金额
         * time_out int 订单超时时间，单位分钟
         * }
         */
        function addOrder(userId,seller_id,goods_id,goods_array,goods_nums,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("Order/Add",{user_id:userId,seller_id:seller_id,goods_id:goods_id,goods_array:goods_array,goods_nums:goods_nums},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.defaultResponse)
                    } else {
                        console.log("addOrder code:"+ response.code)
                    }
                },function (error) {
                    console.log("addOrder error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 删除订单
         * @param order_id
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function delOrder(order_id,token){
            var deferred = $q.defer();
            HttpUtilService.httpGetHasToken("Order/Del",{order_id:order_id},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.response)
                    } else {
                        console.log("delOrder code:"+ response.code)
                    }
                },function (error) {
                    console.log("delOrder error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 修改订单
         * @param order_id
         * @param goods_id
         * @param goods_array
         * @param goods_nums
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         * {
         * order_id int 订单ID
         * real_price float 实付金额
         * time_out int 订单超时时间，单位分钟
         * }
         */
        function resetOrder(order_id,goods_id,goods_array,goods_nums,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("Order/Modify",{order_id:order_id,goods_id:goods_id,goods_array:goods_array,goods_nums:goods_nums},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.defaultResponse)
                    } else {
                        console.log("addOrder code:"+ response.code)
                    }
                },function (error) {
                    console.log("addOrder error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 获取收藏商品列表
         * @param user_id
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         */
        function getCollection(user_id,token){
            var deferred = $q.defer();
            HttpUtilService.httpGetHasToken("Collection/Query",{user_id:user_id},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.RequestTokenMessage)
                    } else {
                        console.log("getCollection code:"+ response.code)
                    }
                },function (error) {
                    console.log("getCollection error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 增加收藏
         * @param user_id
         * @param seller_id
         * @param seller_name 商家名字
         * @param goods_id int
         * @param picture 商品地址
         * @param goods_array
         * @param real_price float 实付金额
         * @param goods_price float 商品原价
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         * {
         * collection_id int 收藏ID
         * response int 是否成功（1：成功；0：未成功；-1：异常或不存在该请求）
         * }
         */
        function addCollection(user_id,seller_id,seller_name,goods_id,picture,goods_array,real_price,goods_price,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("Collection/Add",{user_id:user_id,seller_id:seller_id,seller_name:seller_name,goods_id:goods_id,picture:picture,goods_array:goods_array,real_price:real_price,goods_price:goods_price},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.RequestTokenMessage)
                    } else {
                        console.log("addOrder code:"+ response.code)
                    }
                },function (error) {
                    console.log("addOrder error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 删除收藏
         * @param collection_id
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         * response int 是否成功（1：成功；0：未成功；-1：异常或不存在该请求）
         */
        function delCollection(collection_id,token){
            var deferred = $q.defer();
            HttpUtilService.httpGetHasToken("Collection/Del",{collection_id:collection_id},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.RequestTokenMessage)
                    } else {
                        console.log("delOrder code:"+ response.code)
                    }
                },function (error) {
                    console.log("delOrder error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 获取验证码
         * @param SSO_type int 认证类型,数字1表示使用用户名认证，数字2表示使用用户邮箱认证，数字3表示使用手机号认证
         * @param customer_flag 用户标识？
         * @param type int 1、获取注册验证码；2、找回密码验证码
         * {
         * code int 获取6位验证码
         * }
         */
        function getMegCode(SSO_type,customer_flag,type){
            var deferred = $q.defer();
            HttpUtilService.httpPostNoToken("MegCode/Request",{SSO_type:SSO_type,customer_flag:customer_flag,type:type})
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.RequestTokenMessage)
                    } else {
                        console.log("getMegCode code:"+ response.code)
                    }
                },function (error) {
                    console.log("getMegCode error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 验证校验码
         * @param SSO_type int
         * @param customer_flag
         * @param code 验证码
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         * {
         * response int 是否成功（1：成功；0：未成功；-1：异常或不存在该请求）
         * }
         */
        function checkMegCode(SSO_type,customer_flag,code){
            var deferred = $q.defer();
            HttpUtilService.httpPostNoToken("MegCode/Check",{SSO_type:SSO_type,customer_flag:customer_flag,code:code})
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.RequestTokenMessage)
                    } else {
                        console.log("checkMegCode code:"+ response.code)
                    }
                },function (error) {
                    console.log("checkMegCode error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 获取优惠券
         * @param user_id
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         * {
         * type int 1、通用优惠卡；2、各店优惠券
         * status int 0：未使用1：已使用2：已过期
         * value int 抵用现金多少
         * condition int 100意思为满100可用
         * name 显示名字
         * beginTime 起始时间
         * endTime 结束时间
         * shopId int  会员编号
         * }
         */
        function getCoupons(user_id,token){
            var deferred = $q.defer();
            HttpUtilService.httpGetHasToken("Coupons/Query",{user_id:user_id},token)
                .then(function (response) {
                deferred.resolve(response.data)
                },function (error) {
                    console.log("getCoupons error:"+ error)
                })
            return deferred.promise;
        }

        /***
         * 验证优惠券
         * @param coupons_code 卷号
         * @param coupons_password 密码
         * @param token
         * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
         * {
         * response int 是否成功（1：成功；0：未成功；-1：异常或不存在该请求）
         * }
         */
        function checkCoupons(coupons_code,coupons_password,token){
            var deferred = $q.defer();
            HttpUtilService.httpPostHasToken("Coupons/InviteCoupons",{coupons_code:coupons_code,coupons_password:coupons_password},token)
                .then(function (response) {
                    if(response.code == "000000"){
                        deferred.resolve(response.RequestTokenMessage)
                    } else {
                        console.log("checkCoupons code:"+ response.code)
                    }
                },function (error) {
                    console.log("checkCoupons error:"+ error)
                })
            return deferred.promise;
        }
        return {
            getUserInfo:getUserInfo,
            registerUser:registerUser,
            modifyUser:modifyUser,
            findPasssword:findPasssword,
            marksUser:marksUser,
            realInfoUser:realInfoUser,
            uploadHeadImg:uploadHeadImg,
            getAddressInfo:getAddressInfo,
            delAddress:delAddress,
            addAddress:addAddress,
            getOrderInfo:getOrderInfo,
            addOrder:addOrder,
            resetOrder:resetOrder,
            getCollection:getCollection,
            addCollection:addCollection,
            delCollection:delCollection,
            getMegCode:getMegCode,
            checkMegCode:checkMegCode,
            getCoupons:getCoupons,
            checkCoupons:checkCoupons
        };
    })

